package com.lyz.monitor.impl;

import org.apache.log4j.Logger;

import com.lyz.core.impl.TransferService;
import com.lyz.monitor.AbstractMonitorData;
import com.lyz.monitor.IMonitor;

public class ThreadMonitorData extends AbstractMonitorData {
	
	private static final Logger logger = Logger.getLogger(ThreadMonitorData.class);
	
	public ThreadMonitorData(String tag, TransferService service){
		
		this.tag = tag;
		this.obj = service;
	}

	@Override
	public int need2Alert() {
		
		try {
			if(this.obj == null){
				this.setAletMsg(tag+" thread is null!");
				return IMonitor.LEVEL_MESSAGE;
			}
			
			if(!(this.obj instanceof TransferService)){
				
				logger.error("paramter is not a instance of TransferService");
				
				return -1;
			}
			
			TransferService thread = (TransferService)this.obj;
			/*线程死了，则直接发送短信报警*/
			if(!thread.isAlive()){
				
				logger.error(tag+" thread is dead!");
				this.setAletMsg(tag+" thread is dead!");
				return IMonitor.LEVEL_MESSAGE;
			}
			
			return -1;
		} catch (Exception e) {
			logger.error("fail to get need2Alert",e);
			return -1;
		}
	}

}
